#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mouseState = false;
    m_pos = QPoint(0,0);
    t_pos = QPoint(0,0);

    m_r = 0;
    m_g = 0;
    m_b = 0;
    setMouseTracking(true);
    QImage t_image;
    t_image.load("D:\\Pictures\\002.PNG");
    setQImage(t_image);

}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::mouseMoveEvent(QMouseEvent *event){
    t_pos = event->pos();
    float tx = ((float)t_pos.x())/((float)this->width());
    float ty = ((float)t_pos.y())/((float)this->height());
    m_pos.setX(tx*m_image.width());
    m_pos.setY(ty*m_image.height());
    QColor trgb = m_image.pixelColor(m_pos);
    m_r = trgb.red();
    m_g = trgb.green();
    m_b = trgb.blue();
    update();
}


void MainWindow::enterEvent(QEvent *event){
    mouseState = true;
}


void MainWindow::leaveEvent(QEvent *event){
    mouseState = false;
    update();
}

void MainWindow::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    painter.setBrush(QColor("blue"));
    painter.drawRect(0,0,this->width(),this->height());

    QImage t_image = m_image.scaled(this->width(),this->height());
    painter.drawPixmap(0,0,this->width(),this->height(),QPixmap::fromImage(t_image));

    // 显示图片行和列坐标和当前RGB
    if(mouseState == true){
        painter.setPen(QColor("RED"));
        painter.drawText(t_pos,QString("X,Y:[%1,%2]:rgb(%3,%4,%5)").arg(m_pos.x()).arg(m_pos.y()).arg(m_r).arg(m_g).arg(m_b));

    }
}

void MainWindow::setQImage(QImage p_image){
    m_image = p_image;
    update();

}








